// app.js
import agriknow from './utils/api.js'
const util=require('./utils/util.js')
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  // 用户页面访问埋点
  onShow(){
    // if(wx.getStorageSync('userLogin').userId&&!wx.getStorageSync('userInfo')){
    //   let data = {
    //     id:wx.getStorageSync('userLogin').userId
    //   }
    //   new agriknow().userHome(data).then(res=>{
    //     wx.setStorageSync('userInfo',res.data)
    //   })
    // }
    
    
  },
  globalData: {
    userInfo: null
  },
  onShareAppMessage() {

  },
  onShareTimeline(){
  },
  agriknow:new agriknow(),

  //公共获取列表
  getList:function(url,data,that){
    this.agriknow.commRequest(url,data).then(res=>{
      console.log(res.data)
      var datas=res.data
      var list=datas.list || []
      //console.log(datas.list,'datas.list')
      if(url=='ht/store/withdrawalPage'){
        list.map(item=>{
          item.createTime=item.createTime.substring(0,10)
        })
      }
      if(url=='ht/store/incomePage'){
        list.map(item=>{
          item.createTime=util.formatsDates(new Date(item.createTime.replace(/-/g, '/')))
          item.balance=item.balance.toFixed(2)
        })
      }
      if(url=='ht/store/shareList'){
        list.map(item=>{
          if(!item.distance){
            item.distance=0
          }
          item.distance=Number(Number(item.distance)/1000).toFixed(1)+'km'
        })
      }
      var lists=that.data.lists.concat(list)
      that.setData({
        lists:lists,
        isShow:true
      })



      if(that.data.lists.length==0){
        that.setData({
          empty:true
        })
      }else{
        that.setData({
          empty:false
        })
      }
      
      if(that.data.page>=1 && datas.isLastPage){
      
        that.setData({
          isLoadingAll:true
        })
      }
    })
},


})
